﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbobsCOM;
using System.IO;

namespace CompanySyncProject
{
    class DISapBobs : BaseRun
    {
        public DISapBobs(Company co, System.IO.StreamWriter lo)
            : base(co, lo)
        {

        }

        public override void Run()
        {
            oCompany.XmlExportType = BoXmlExportTypes.xet_ExportImportMode;
            /*
            SAPbobsCOM.ItemPriceParams param = oCompany.GetCompanyService().GetDataInterface(CompanyServiceDataInterfaces.csdiItemPriceParams);
            param.CardCode = "C001";
            param.ItemCode = "I002";
            param.UoMEntry = 1;
            param.Date = DateTime.Now;

            string xml = param.ToXMLString();

            SAPbobsCOM.ItemPriceReturnParams rtnParam = oCompany.GetCompanyService().GetItemPrice(param);
            string currency = rtnParam.Currency;
            double price = rtnParam.Price;
            double discount = rtnParam.Discount;
            */
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict["CardCode"] = "C001";
            dict["ItemCode"] = "I002";
            dict["UoMEntry"] = "2";

            string paramName = "ItemPriceParams";
            string strOutput = "";
            string content = "";
            string template = "<{0}>{1}</{0}>";
            foreach (KeyValuePair<string, string> kv in dict)
            {
                content += (string.Format(template, kv.Key, kv.Value));
            }
            strOutput = string.Format(template, paramName, content);

            SAPbobsCOM.ItemPriceParams param1 = oCompany.GetCompanyService().GetDataInterface(CompanyServiceDataInterfaces.csdiItemPriceParams);
            //param1.FromXMLString("<ItemPriceParams><Date>2015-11-18</Date><UoMEntry>1</UoMEntry><BlanketAgreementNumber nil=\"true\"></BlanketAgreementNumber><BlanketAgreementLine nil=\"true\"></BlanketAgreementLine><UoMQuantity nil=\"true\"></UoMQuantity><InventoryQuantity nil=\"true\"></InventoryQuantity><Currency nil=\"true\"></Currency><ItemCode>I002</ItemCode><CardCode>C001</CardCode><PriceList nil=\"true\"></PriceList></ItemPriceParams>");
            param1.FromXMLString(strOutput);
            SAPbobsCOM.ItemPriceReturnParams rtnParam1 = oCompany.GetCompanyService().GetItemPrice(param1);
            string output = rtnParam1.ToXMLString();

            Dictionary<string, string> dictRtn = new Dictionary<string, string>();
            MemoryStream ms = new MemoryStream();
            ms.Position = 0;
            StreamWriter sw = new StreamWriter(ms, Encoding.Unicode);
            sw.Write(output);
            sw.Flush();
            ms.Position = 0;
            System.Xml.Linq.XDocument xXML = System.Xml.Linq.XDocument.Load(ms);
            System.Xml.Linq.XElement root = (System.Xml.Linq.XElement)xXML.Root;
            var query = from n in root.Nodes()
                        select n;
            foreach (var no in query)
            {
                System.Xml.Linq.XElement xNo = (System.Xml.Linq.XElement)no;
                string name = xNo.Name.ToString();
                string value = xNo.Value.ToString();
                dictRtn.Add(name, value);
            }
            
            string jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(dictRtn);

        }
    }
}
